﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dapper;

namespace Sqlite3Demo.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
            //sb.DataSource = @"D:\\sqlite.s3db";
            var dbConnection = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + @"App_Data\sqlite3test.db;Version=3";
            sb.DataSource = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\sqlite3test.db";
            SQLiteConnection con = new SQLiteConnection(sb.ToString());
            con.Open();

            var data = con.Query("select * from mytable");

            con.Close();
            return View();
        }

        public ActionResult About()
        {
            SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
            sb.DataSource = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\sqlite3test.db";
            SQLiteConnection con = new SQLiteConnection(sb.ToString());
            var id = con.Execute(" insert into mytable(id,value)values(@id,@value);SELECT LAST_INSERT_ROWID() FROM mytable"
                , new
                {
                    id = 2,
                    value = "b"
                });
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}